<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/31
 * Time: 14:33
 */

namespace osc\mobile\controller;

use osc\admin\controller\EndRemind;
use think\Db;
use think\Session;
use think\Loader;
use think\File;
use think\Request;
use osc\mobile\model\Member;

class Useticket  extends MobileBase
{

    /**
     * 验证是否登录
     */
    protected function _initialize()
    {

        parent::_initialize();
        define('UID',is_login());

        if (!Session::has("username")) {

            $this->redirect('login/login');

        }



    }

    
    public function index(){
    

        $this->assign('top_title','确认提取现货');
        $this->assign('SEO',['title'=>'确认提取现货-'.config('SITE_TITLE')]);

        $this->assign('flag','user');

        return $this->fetch();
    }

    public function use(){
    

        $this->assign('top_title','确认提取现货');
        $this->assign('SEO',['title'=>'确认提取现货-'.config('SITE_TITLE')]);

        $this->assign('flag','user');

        return $this->fetch();
    }

   


    /**
     * 元宝兑换
     * @return array|mixed
     */
    public function change()
    {
        //$this->error('暂未开放');
		//return ['error'=>'暂未开放'];
        $username = session('username');

        if(request()->isPost()){

            $data=input('post.');

            $time = date('Hi',time());
            
            /*if($time > 930 && $time < 1530){
                return ['error'=>'不在申请时间段，请于每日9:30之前，或15:30之后进行申请'];
            }*/
            //进行数据判断
            if($data['exchange_id']=="")
            {
                return array('error'=>'请选择交易所');
            }
            if($data['variety_id']=="")
            {
                return array('error'=>'请选择交易品种');
            }

            if($data['number']=="")
            {
                return array('error'=>'请输入转换数量');
            }

            if(!(floor($data['number'])==$data['number']))
            {
                return array('error'=>'请输入正确的数量!');
            }

            if($data['number']<1)
            {
                return array('error'=>'请输入正确的数量');
            }

            $pwd2 = think_ucenter_encrypt($data['pwd2'],config('PWD_KEY'));
            $user=Db::name('member')->where('username',$username)->find();
            if($pwd2!=$user['2password'])
            {
                return array('error'=>'二级密码输入有误');
            }

            //查询交易所账号
            $acc = Db::name('exchange_trade_acc')->where('username',$username)->where('exchange_id',$data['exchange_id'])->find();
            if(!$acc){
                return array('error'=>'请先绑定交易所账号','url'=>'/mobile/user/exchange_trade');
            }

            //判断是否有还没有结束的
            $res = Db::name('jilin_ticket')->where('username',$username)->where('state',0)->find();
            if($res)
            {
                return array('error'=>'您上一次申请还没有处理完毕!');
            }

            //查询收盘价
            $var=Db::name('value_config')->where('variety_id',$data['variety_id'])->order('dt desc')->find();

            //查询发行价
            $issue = Db::name('contract_issue')->where('variety_id',$data['variety_id'])->find();


            $sum = ceil(($var['rate'] -$issue['issue'])*$data['number']);

            if($sum > $user['jilin_bean'])
            {
                $num = floor($user['jilin_bean']/($var['rate'] -$issue['issue']));

                $error = '元宝不够，最大换购数为：'.$num;
                return array('error'=> $error,'url'=>'/mobile');
            }
            $map = [
                'token'		=> $data['token'],
                'username'	=> $username,
                'exchange_id'	=> $data['exchange_id'],
                'variety_id'	=> $data['variety_id'],
                'issue'			=> $var['rate'],
                'money'			=> $sum,
                'num'			=> $data['number'],
                'create_time'		=> time(),
            ];

            //存储兑换记录
            $res = Db::name('jilin_ticket')->insert($map);

            $member = new Member();

            //减少用户元宝数
            $red = $member->where('username',$username)->setDec('jilin_bean',$map['money']);

            if(!$res || !$red){
                if($res){
                    Db::name('jilin_ticket')->where('id',$res)->delete();
                }
                if($red){
                    $member->where('username',$username)->setInc('jilin_bean',$map['money']);
                }
                return array('error'=>'申请兑换失败','url'=>'/mobile');
            }
            return array('success'=>'申请兑换成功','url'=>'/mobile');
        }

        $start = input('param.start');
        $end = input('param.end');
        if($start && $end){
            $map = [
                'username' => $username,
                'create_time' => array(['>',strtotime($start)],['<',strtotime($end)])
            ];
        }
        $map['username'] = $username;
        $list = Db::name('jilin_ticket')
            ->where($map)
            ->order('create_time desc')
            ->paginate(10,false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ]);

        $page=$list->render(); //获取分页
        $exchange = Db::name('exchange_house')->select();
        $variety = Db::name('exchage_variety')->select();
//		print_r($variety);exit;
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('exchange',$exchange);
        $this->assign('variety',$variety);
        $this->assign('top_title','交易品兑换');
        return $this->fetch();
    }


    

}